home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 223_01 / cc22.c < prev    next >
Text File  |  1980-01-01  |  8KB  |  383 lines

  1. ifline() {
  2.   while(1) {
  3.     inline();
  4.     if(eof) return;
  5.     if(match("#ifdef")) {
  6.       ++iflevel;
  7.       if(skiplevel) continue;
  8.       symname(msname, NO);        /*19*/
  9.       if(search(msname, macn, NAMESIZE+2, MACNEND, MACNBR, 0)==0)
  10.                                       /*19*/
  11.         skiplevel=iflevel;
  12.       continue;
  13.       }
  14.     if(match("#ifndef")) {
  15.       ++iflevel;
  16.       if(skiplevel) continue;
  17.       symname(msname, NO);     /*19*/
  18.       if(search(msname, macn, NAMESIZE+2, MACNEND, MACNBR, 0))
  19.                                       /*19*/
  20.         skiplevel=iflevel;
  21.       continue;
  22.       }
  23.     if(match("#else")) {
  24.       if(iflevel) {
  25.         if(skiplevel==iflevel) skiplevel=0;
  26.         else if(skiplevel==0)  skiplevel=iflevel;
  27.         }
  28.       else noiferr();
  29.       continue;
  30.       }
  31.     if(match("#endif")) {
  32.       if(iflevel) {
  33.         if(skiplevel==iflevel) skiplevel=0;
  34.         --iflevel;
  35.         }
  36.       else noiferr();
  37.       continue;
  38.       }
  39.     if(skiplevel) continue;
  40.  
  41.     if(listfp){                    /*2.2 fas*/
  42.       if(listfp == output) cout(';', output);
  43.       lout(line, listfp);
  44.       }
  45.  
  46.     if(ch==0) continue;
  47.     break;
  48.     }
  49.   }
  50.  
  51. keepch(c)  char c; {
  52.   if(pptr<LINEMAX) pline[++pptr]=c;
  53.   }
  54.  
  55. preprocess() {
  56.   int k;
  57.   char c;
  58.   if(ccode) {
  59.     line=mline;
  60.     ifline();
  61.     if(eof) return;
  62.     }
  63.   else {
  64.     line=pline;
  65.     inline();
  66.     return;
  67.     }
  68.   pptr = -1;
  69.   while(ch != NEWLINE && ch) {             /*23,32*/
  70.     if(white()) {
  71.       keepch(' ');
  72.       while(white()) gch();
  73.       }
  74.     else if(ch=='"') {
  75.       keepch(ch);
  76.       gch();
  77.       while((ch!='"')|((*(lptr-1)==92)&(*(lptr-2)!=92))) {
  78.         if(ch==0) {
  79.           error("no quote");
  80.           break;
  81.           }
  82.         keepch(gch());
  83.         }
  84.       gch();
  85.       keepch('"');
  86.       }
  87.     else if(ch==39) {
  88.       keepch(39);
  89.       gch();
  90.       while((ch!=39)|((*(lptr-1)==92)&(*(lptr-2)!=92))) {
  91.         if(ch==0) {
  92.           error("no apostrophe");
  93.           break;
  94.           }
  95.         keepch(gch());
  96.         }
  97.       gch();
  98.       keepch(39);
  99.       }
  100.     else if((ch=='/')&(nch=='*')) {
  101.       bump(2);
  102.       while(((ch=='*')&(nch=='/'))==0) {
  103.         if(ch) bump(1);
  104.         else {
  105.           ifline();
  106.           if(eof) break;
  107.           }
  108.         }
  109.       bump(2);
  110.       }
  111.     else if(an(ch)) {
  112.       k=0;
  113.       while((an(ch)) & (k<NAMEMAX)) {         /*07*/
  114.         msname[k++]=ch;                       /*07*/
  115.         gch();
  116.         }
  117.       msname[k]=0;
  118.       if(search(msname, macn, NAMESIZE+2, MACNEND, MACNBR, 0)) {
  119.         k=getint(cptr+NAMESIZE, 2);
  120.         while(c=macq[k++]) keepch(c);
  121.         while(an(ch)) gch();                  /*07*/
  122.         }
  123.       else {
  124.         k=0;
  125.         while(c=msname[k++]) keepch(c);
  126.         }
  127.       }
  128.     else keepch(gch());
  129.     }
  130.   if(pptr>=LINEMAX) error("line too long");
  131.   keepch(0);
  132.   line=pline;
  133.   bump(0);
  134.   }
  135.  
  136. noiferr() {
  137.   error("no matching #if...");
  138.   errflag=0;
  139.   }
  140.  
  141. addmac() {
  142.   int k;
  143.   if(symname(msname, NO)==0) {
  144.     illname();
  145.     kill();
  146.     return;
  147.     }
  148.   k=0;
  149.   if(search(msname, macn, NAMESIZE+2, MACNEND, MACNBR, 0)==0) {
  150.     if(cptr2=cptr) while(*cptr2++ = msname[k++]);
  151.     else {
  152.       error("macro name table full");
  153.       return;
  154.       }
  155.     }
  156.   putint(macptr, cptr+NAMESIZE, 2);
  157.   while(white()) gch();
  158.   while(putmac(gch()));
  159.   if(macptr>=MACMAX) {
  160.     error("macro string queue full"); abort(ERRCODE);
  161.     }
  162.   }
  163.  
  164. putmac(c)  char c; {
  165.   macq[macptr]=c;
  166.   if(macptr<MACMAX) ++macptr;
  167.   return c;
  168.   }
  169.  
  170. /*
  171. ** search for symbol match
  172. ** on return cptr points to slot found or empty slot
  173. */
  174. search(sname, buf, len, end, max, off)
  175.   char *sname, *buf, *end;  int len, max, off; {
  176.   cptr=cptr2=buf+((hash(sname)%(max-1))*len);
  177.   while(*cptr != 0) {
  178.     if(astreq(sname, cptr+off, NAMEMAX)) return 1;
  179.     if((cptr=cptr+len) >= end) cptr=buf;
  180.     if(cptr == cptr2) return (cptr=0);
  181.     }
  182.   return 0;
  183.   }
  184.  
  185. hash(sname) char *sname; {
  186.   int i, c;
  187.   i=0;
  188.   while(c = *sname++) i=(i<<1)+c;
  189.   return i;
  190.   }
  191.  
  192. setstage(before, start) int *before, *start; {
  193.   if((*before=stagenext)==0) stagenext=stage;
  194.   *start=stagenext;
  195.   }
  196.  
  197. clearstage(before, start) char *before, *start; {
  198.   *stagenext=0;
  199.   if(stagenext=before) return;
  200.   if(start) {
  201. #ifdef OPTIMIZE
  202.     peephole(start);
  203. #else
  204.     sout(start, output);
  205. #endif
  206.     }
  207.   }
  208.  
  209. outdec(number)  int number; {
  210.   int k,zs;
  211.   char c, *q, *r;                                 /*09*/
  212.   zs = 0;
  213.   k=10000;
  214.   if (number<0) {
  215.     number=(-number);
  216.     outbyte('-');
  217.     }
  218.   while (k>=1) {
  219.     q=0; r=number;                                /*09*/
  220.     while(r >= k) {++q; r -= k;}                  /*09*/
  221.     c = q + '0';                                  /*09*/
  222.     if ((c!='0')|(k==1)|(zs)) {
  223.       zs=1;
  224.       outbyte(c);
  225.       }
  226.     number=r;                                     /*09*/
  227.     k=k/10;
  228.     }
  229.   }
  230.  
  231. ol(ptr)  char ptr[];  {
  232.   ot(ptr);
  233.   nl();
  234.   }
  235.  
  236. ot(ptr) char ptr[]; {
  237.   outstr(ptr);
  238.   }
  239.  
  240. outstr(ptr) char ptr[]; {
  241.   poll(1); /* allow program interruption */
  242.   /* must work with symbol table names terminated by length */
  243.   while(*ptr >= ' ') outbyte(*ptr++);
  244.   }
  245.  
  246. outbyte(c) char c; {
  247.   if(stagenext) {
  248.     if(stagenext==stagelast) {
  249.       error("staging buffer overflow");
  250.       return 0;
  251.       }
  252.     else *stagenext++ = c;
  253.     }
  254.   else cout(c,output);
  255.   return c;
  256.   }
  257.  
  258. cout(c, fd) char c; int fd; {
  259.   if(fputc(c, fd)==EOF) xout();
  260.   }
  261.  
  262. sout(string, fd) char *string; int fd; {
  263.   if(fputs(string, fd)==EOF) xout();
  264.   }
  265.  
  266. lout(line, fd) char *line; int fd; {
  267.   sout(line, fd);
  268.   cout(NEWLINE, fd);                    /*23*/
  269.   }
  270.  
  271. xout() {
  272.   fputs("output error", stderr);            /*23*/
  273.   abort(ERRCODE);
  274.   }
  275.  
  276. nl() {
  277.   outbyte(NEWLINE);                    /*23*/
  278.   }
  279.  
  280. col() {
  281. #ifdef COL
  282.   outbyte(':');
  283. #endif
  284.   }
  285.  
  286. error(msg) char msg[]; {
  287.   if(errflag) return; else errflag=1;
  288.   lout(line, stderr);
  289.   errout(msg, stderr);
  290.   if(alarm) fputc(7, stderr);
  291.   if(pause) while(fgetc(stderr)!=NEWLINE);        /*23*/
  292.   if(listfp>0) errout(msg, listfp);
  293.   }
  294.  
  295. errout(msg, fp) char msg[]; int fp; {
  296.   int k; k=line+2;
  297.   while(k++ <= lptr) cout(' ', fp);
  298.   lout("/\\", fp);
  299.   sout("**** ", fp); lout(msg, fp);
  300.   }
  301.  
  302. streq(str1,str2)  char str1[],str2[]; {
  303.   int k;
  304.   k=0;
  305.   while (str2[k]) {
  306.     if ((str1[k])!=(str2[k])) return 0;
  307.     ++k;
  308.     }
  309.   return k;
  310.  }
  311.  
  312. astreq(str1,str2,len)  char str1[],str2[];int len; {
  313.   int k;
  314.   k=0;
  315.   while (k<len) {
  316.     if ((str1[k])!=(str2[k]))break;
  317.     /*
  318.     ** must detect end of symbol table names terminated by
  319.     ** symbol length in binary
  320.     */
  321.     if(str1[k] < ' ') break;
  322.     if(str2[k] < ' ') break;
  323.     ++k;
  324.     }
  325.   if (an(str1[k]))return 0;
  326.   if (an(str2[k]))return 0;
  327.   return k;
  328.  }
  329.  
  330. match(lit)  char *lit; {
  331.   int k;
  332.   blanks();
  333.   if (k=streq(lptr,lit)) {
  334.     bump(k);
  335.     return 1;
  336.     }
  337.   return 0;
  338.   }
  339.  
  340. amatch(lit,len)  char *lit;int len; {
  341.   int k;
  342.   blanks();
  343.   if (k=astreq(lptr,lit,len)) {
  344.     bump(k);
  345.     while(an(ch)) inbyte();
  346.     return 1;
  347.     }
  348.   return 0;
  349.  }
  350.  
  351. nextop(list) char *list; {
  352.   char op[4];
  353.   opindex=0;
  354.   blanks();
  355.   while(1) {
  356.     opsize=0;
  357.     while(*list > ' ') op[opsize++] = *list++;
  358.     op[opsize]=0;
  359.     if(opsize=streq(lptr, op))
  360.       if((*(lptr+opsize) != '=')&
  361.          (*(lptr+opsize) != *(lptr+opsize-1)))
  362.          return 1;
  363.     if(*list) {
  364.       ++list;
  365.       ++opindex;
  366.       }
  367.     else return 0;
  368.     }
  369.   }
  370.  
  371. blanks() {
  372.   while(1) {
  373.     while(ch) {
  374.       if(white()) gch();
  375.       else return;
  376.       }
  377.     if(line==mline) return;
  378.     preprocess();
  379.     if(eof)break;
  380.     }
  381.   }
  382.  
  383.